using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;

namespace Framework.DataObjects
{
    public abstract class BaseXmlParameter
    {
        protected BaseXmlParameter()
        {
            Parameters = new List<NameTextValueDo>();
        }

        [XmlElement("Parameter")]
        public List<NameTextValueDo> Parameters { get; set; }

        // *********************************************************************************
        // Methods
        // *********************************************************************************

        /// <summary>
        /// Set the named parameter with a value.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        public void SetParameter<T>(string name, T value)
        {
            SetParameter(-1, name, value);
        }

        /// <summary>
        /// Set the named parameter with a value.
        /// </summary>
        /// <param name="insertIndex">Index of the insert.</param>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        public void SetParameter<T>(int insertIndex, string name, T value)
        {
            var param = GetParameter(name);

            var storeValue = Is.IsNull(value) ? string.Empty : value.ToString();

            if (param == null)
            {
                param = new NameTextValueDo(name, storeValue);

                if (insertIndex == -1 || insertIndex > Parameters.Count - 1)
                {
                    Parameters.Add(param);
                }
                else
                {
                    Parameters.Insert(insertIndex, param);
                }
            }
            else
            {
                param.Value = storeValue;
            }
        }

        /// <summary>
        /// Get the parameter as a value, defaults to String.Empty if not found.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public string GetParameterValue(string name)
        {
            return GetParameterValue(name, String.Empty);
        }

        /// <summary>
        /// Get the parameter as a value, uses a default value if not found.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns></returns>
        public string GetParameterValue(string name, string defaultValue)
        {
            var param = GetParameter(name);

            return (param == null ? defaultValue : StrUtil.DefaultValue(param.Value, defaultValue));
        }

        /// <summary>
        /// Gets the parameter.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public NameTextValueDo GetParameter(string name)
        {
            return Parameters.FirstOrDefault(parameter => Is.EqualString(parameter.Name, name));
        }
    }
}